<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.products_filter.php 409 2014-01-18 16:13:07Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 409 $
* 	zuletzt geaendert von:	$Author: $
* 	Datum:					$Date: 2014-01-18 16:13:07 +0000 (Sat, 18 Jan 2014) $
*
* 	extends by dirk.mueller (edv-mueller.com)
* 	http://www.itdotmedia.de
*
* --------------------------------------------------------------------------------------
* 	based on:
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
* 	Copyright (c) since 2011 SEO:mercari
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
class products_filter {
  var $db;
  
  var $price_min, $price_max;
  var $filters;
  var $filter_name;
  
  var $products_id;
  
	function __construct() {
    global $db;
    
    $this->db = $db;	 
    $this->products_id = array();

		$_REQUEST = array_merge($_REQUEST, $_GET);
	}

  function get_price_bounds($cat_id, $deep=false) {

		$sql = "
      select
      	min(p.products_price) _min, 
      	max(p.products_price) _max, 
      	min(p.products_price*(100.0+t.tax_rate)/100.0) _mintax, 
      	max(p.products_price*(100.0+t.tax_rate)/100.0) _maxtax 
      from
      	".TABLE_PRODUCTS_TO_CATEGORIES." ptc
      	inner join ".TABLE_PRODUCTS." p on p.products_id = ptc.products_id
      	inner join ".TABLE_TAX_RATES." t on t.tax_rates_id = p.products_tax_class_id
      where
      	p.products_status = 1
      	and ptc.categories_id = ".$cat_id."      
    ";
    $prices = $this->db->db_query($sql);
		if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0)
			return array('min' => floor($prices->fields['_min']), 'max' => ceil($prices->fields['_max']));
		else
			return array('min' => floor($prices->fields['_mintax']), 'max' => ceil($prices->fields['_maxtax']));
  }
  
  function set_filter_name() {
    $this->filter_name = array();

    if(
      (isset($_GET['filter']) && !empty($_GET['filter']))
	     || (isset($_GET['sub_filter']) && !empty($_GET['sub_filter']))
	     || isset($_GET['minp']))
    {

    	if(isset($_GET['minp']) || isset($_GET['maxp']))
        $this->filter_name[] = 'Preis: ' . $price->format($this->price_min, true)
                             . ' - ' . $price->format($this->price_max, true);
    }
    
  }

  function set_request() {
		list($req_filter, $req_sub_filter, $req_minp, $req_maxp) = $this->get_request();

		$_SESSION['product_filter']['cPath'] = $_GET['cPath'];
		$_SESSION['product_filter']['minp'] = $req_minp;
		$_SESSION['product_filter']['maxp'] = $req_maxp;
		$_SESSION['product_filter']['sub_filter'] = $req_sub_filter;

  }
  
  function get_filter_param() {
    $filter_param = array();

		list($req_filter, $req_sub_filter, $req_minp, $req_maxp) = $this->get_request();
		if(!empty($req_minp)) $filter_param[] = 'minp='.$req_minp;
		if(!empty($req_maxp)) $filter_param[] = 'maxp='.$req_maxp;
		if(!empty($req_sub_filter)) {
			foreach($req_sub_filter as $req_sub_filter_item) 
				$filter_param[] = 'sub_filter[]='.$req_sub_filter_item;
		}

		if(!empty($filter_param))
			$filter_param = '?' . explode('&', $filter_param);
		else
			$filter_param = '?filter=n';
  }
	
  function get_request() {
		if ( 
				!isset($_POST['action'])
				&& (isset($_SESSION['product_filter']['cPath']) && isset($_GET['cPath'])
						&& $_SESSION['product_filter']['cPath'] == $_GET['cPath'] )
					)	{
			$_REQUEST = array_merge($_SESSION['product_filter'], $_REQUEST);
		}
		
		$req_filter = isset($_REQUEST['filter']) ? $_REQUEST['filter'] : '';
		$req_sub_filter = isset($_REQUEST['sub_filter']) ? $_REQUEST['sub_filter'] : '';
		$req_minp = isset($_REQUEST['minp']) ? $_REQUEST['minp'] : 0;
		$req_maxp = isset($_REQUEST['maxp']) ? $_REQUEST['maxp'] : 0;
		return array($req_filter, $req_sub_filter, $req_minp, $req_maxp);
	}
  
  function get_products_id() {
    if (count($this->products_id) > 0) return $this->products_id;
		list($req_filter, $req_sub_filter, $req_minp, $req_maxp) = $this->get_request();

    if( !empty($req_filter) || !empty($req_sub_filter) || !empty($req_minp)) {

      // price
      $price_bound = $this->db->db_query("select max(tbl.products_price)*2.0 _max from ".TABLE_PRODUCTS." tbl", true);
    	$this->price_min = empty($req_minp) ? 0 : $req_minp;
    	$this->price_max = empty($req_maxp) ? $price_bound->fields['_max'] : $req_maxp;

      // create groups of filter for AND

      $filter_groups= array();
    	if(!empty($req_sub_filter)) {
        $this->filters = $req_sub_filter;
        $sql = "
          select
            flt.parent_id, group_concat(flt.filter_id) _filters
          from
            ".TABLE_PRODUCTS_FILTERS." flt
          where
            filter_id in ('".implode("', '", $req_sub_filter)."')
          group by
            flt.parent_id";
            
        $filter_groups_query = $this->db->db_query($sql, true);
        if ($filter_groups_query->_numOfRows) {
          while(!$filter_groups_query->EOF) {
            $filter_groups[$filter_groups_query->fields['parent_id']] = $filter_groups_query->fields['_filters'];
            $filter_groups_query->MoveNext();
          }
        }
        //echo_e($filter_groups);
      }
      
			if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0)
				$sql_price = "p.products_price";
			else
				$sql_price = "p.products_price*(100+t.tax_rate)/100.0";

			$sql = "
        select
          p.products_id,
          ".$sql_price." _price,
          GROUP_CONCAT(ptf.filter_id) filter_list
        from
          ".TABLE_PRODUCTS_TO_FILTERS." ptf
        	inner join ".TABLE_PRODUCTS." p on p.products_id = ptf.products_id
        	inner join ".TABLE_TAX_RATES." t on t.tax_rates_id = p.products_tax_class_id
        where
          ".$sql_price." >= ".$this->price_min."
          and ".$sql_price." <= ".$this->price_max." 
				group by 
					ptf.products_id
				having 
					1=1 ";

      foreach($filter_groups as $g => $flts) {
      	$arrFlt = array();
      	foreach(explode(',', $flts) as $flt) $arrFlt[] = "find_in_set ('".$flt."', filter_list)";
        $sql .= "and (" . implode(" or ", $arrFlt) . ") ";
      }


      $product_ids_query = $this->db->db_query($sql, true);
      if($product_ids_query->_numOfRows) {
        while(!$product_ids_query->EOF) {
          $this->products_id[] = $product_ids_query->fields['products_id'];
          $product_ids_query->MoveNext();
        }
      }
      return $this->products_id;
    }
    else
      return false;

/*
	$filter_products = '';
	$pd = '';

/*
	if(isset($_GET['filter']) && !empty($_GET['filter'])) {
		foreach($_GET['filter'] AS $name => $item)
			$filter_param .= '&filter[]='.$item;
	}
* /
	if(isset($_GET['sub_filter']) && !empty($_GET['sub_filter'])) {
		foreach($_GET['sub_filter'] AS $sub_name => $sub_item) {
			$filter_param .= '&sub_filter[]='.$sub_item;
			$session_sub_filter[] = $sub_item;
		}
		$in_sub_filters .= '\''.implode('\',\'', $_GET['sub_filter']).'\'';
		$filters .= (strlen(implode(',', $_GET['sub_filter']))-1);


		$pd = create_in_product($p);
		$brand = '';
	}




	}
	if($pIDs = $db->db_query($filter_sql)) {
		while(!$pIDs->EOF) {
			$p_ids[] = $pIDs->fields['products_id'];
			$pIDs->MoveNext();
		}
		$filter_products = create_in_product($p_ids);
		if(isset($_GET['filter_id'])) {
			$getBrand = $db->db_query("SELECT brand_name FROM ".TABLE_BRAND." WHERE brand_id = '".$_GET['filter_id']."' ");
			$brands = $getBrand->fields['brand_name'];
		}

		$filter_title = !empty($filter_names) ? 'Suchparameter: ' . $filter_names : '';

		if(!empty($brands) || !empty($sort))
			$filter_title .= '<br />Suchfilter: '.$brands.(empty($brands)?'':', ').$sort;
	}
---
*/
  }

}